﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class frmStm32updata1 : Form
    {
        frmMain MainForm = null;
        string FileName = "";
        CRC16 CRCData = new CRC16();
        public frmStm32updata1(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
        }
        private void FrmStm32updata1_Load(object sender, EventArgs e)
        {
            if (!MainForm.ForceUpdateFlag)
            {
                btnFileSel.Visible = true;
                btnStm32update.Visible = true;
                Application.DoEvents();
                btnForceUpdate.Visible = false;
            }
        }
        public void btnForceStm32update_Click(object sender, EventArgs e)
        {
            MainForm.ForceUpdateFlag = false;
            FileName = "./update/STM32F7XX_Updata1.10.bin";
            textBox2.Text = FileName;
            MainForm.textBox1.Text += "强制升级...\r\n";
            btnForceUpdate.Enabled = false;
            Application.DoEvents();
            btnStm32update_Click(btnStm32update, e);
            Application.DoEvents();
        }
        public void btnStm32update_Click(object sender, EventArgs e)
        {
            Int32 m_txlen = 0;
            byte[] TemFileContent = new byte[120 * 1024];
            byte[] File_length = new byte[9];
            Int32 ByteReaded = 0;
            FileStream aFile = new FileStream(FileName, FileMode.Open);
            btnStm32update.Enabled = false;
            btnFileSel.Enabled = false;
            File_length[0] = 0x55;
            File_length[1] = 0x01;
            File_length[2] = 0x04;
            File_length[3] = Convert.ToByte(Convert.ToChar((aFile.Length >> 24) % 256));
            File_length[4] = Convert.ToByte(Convert.ToChar((aFile.Length >> 16) % 256));
            File_length[5] = Convert.ToByte(Convert.ToChar((aFile.Length >> 8) % 256));
            File_length[6] = Convert.ToByte(Convert.ToChar((aFile.Length) % 256));
            CRCData.CalculateCrc16(File_length, out File_length[7], out File_length[8]);
            try
            {
                if (MainForm.comPort.IsOpen == false) MainForm.comPort.Open();
                MainForm.comPort.Write(File_length, 0, 9);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            Application.DoEvents();
            System.Threading.Thread.Sleep(300);
            ByteReaded = aFile.Read(TemFileContent, 0, 512);
            MainForm.continueflag = 1;
            button3.Enabled = false;
            while (MainForm.start_trans_flag != 0x00)
            {

                while (ByteReaded > 0)
                {
                    if (MainForm.continueflag == 1)
                    {
                        //MainForm.continueflag = 0;
                        System.Threading.Thread.Sleep(100);
                        if (MainForm.start_trans_flag == 0x00)
                            break;
                        try
                        {
                            if (MainForm.comPort.IsOpen == false) MainForm.comPort.Open();
                            MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        /////第一笔数据多等一等，下位机在进行擦除动作，延时10s
                        if (m_txlen == 0)
                        {
                            Application.DoEvents();
                            System.Threading.Thread.Sleep(10000);
                        }
                        m_txlen += ByteReaded;
                        ByteReaded = aFile.Read(TemFileContent, 0, 512);
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label1.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    }
                }
                MainForm.start_trans_flag = 0x00;
            }

            button3.Enabled = true;
            aFile.Close();
            //btnStm32update.Enabled = true;
            //btnFileSel.Enabled = true;   
            Application.DoEvents();
            MainForm.textBox1.Text += "下方信息栏出现红色闪动后即可关闭升级对话框\r\n";

        }

        private void btnFileSel_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string[] arr = new string[12];
            of.Filter = @"bin文件(*.bin)|*.bin|所有文件(*.*)|*.*"; ;
            of.Title = "载入STM32固件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                FileName = of.FileName;
                textBox2.Text = FileName;
                btnStm32update.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
